<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head>

<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">用户管理</h3>
            </div>
            <div class="panel-body">
                <div>
                    <label>
                        id
                    </label>
                    <input type="text" v-model="uid">
                    <label>
                        name
                    </label>
                    <input type="text" v-model="name">
                    <label>
                        age
                    </label>
                    <input type="text" v-model="age">

                    <button type="button" class="btn btn-primary" @click="addUser()">增加</button>

                </div>
            </div>
        </div>

        <table class="table table-hover table-bordered">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>age</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in users" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.age}}</td>
                </tr>
            </tbody>
        </table>

    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                uid: "",
                name: " ",
                age: " ",
                users: []
            },
            mounted() {
                axios.get(' http://localhost:3000/users')
                    .then(response => (this.users = response.data))
                console.log(this.users)
            },
            methods: {
                addUser() {
                    // var user = { id: this.uid, name: this.name, age: this.age }
                    // this.users.push(user)
                    axios.post('http://localhost:3000/users', {
                        id: this.uid,
                        name: this.name,
                        age: this.age
                    }).then(response => {
                        console.log(response)
                        axios.get(' http://localhost:3000/users')
                            .then(response => (this.users = response.data))
                    })
                    this.id = name = age = ""
                }
            }
        })
    </script>
</body>

</html>